﻿#include "Tornado.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../../Shared/ServerPackets.h"
#include "../MapObject.h"
#include "../../../Shared/Enums.h"

using namespace Server::MirDatabase;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    Tornado::Tornado(MonsterInfo *info) : MonsterObject(info)
    {
    }

    bool Tornado::InAttackRange()
    {
        return getCurrentMap() == getTarget()->getCurrentMap() && Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), AttackRange);
    }

    void Tornado::Attack()
    {
        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));
        bool ranged = getCurrentLocation() == getTarget()->getCurrentLocation() || !Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), 1);

        if (!ranged)
        {
            MonsterObject::Attack();
        }
        else
        {
            S::ObjectRangeAttack *tempVar = new S::ObjectRangeAttack();
            tempVar->ObjectID = ObjectID;
            tempVar->Direction = getDirection();
            tempVar->Location = getCurrentLocation();
            tempVar->Type = 0;
            Broadcast(tempVar);

            std::vector<MapObject*> targets = FindAllTargets(AttackRange, getCurrentLocation(), false);

            if (targets.empty())
            {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
                return;
            }

            for (int i = 0; i < targets.size(); i++)
            {
                MirDirection dir = Functions::DirectionFromPoint(targets[i]->getCurrentLocation(), getCurrentLocation());
                int dist = Functions::MaxDistance(targets[i]->getCurrentLocation(), getCurrentLocation());

                targets[i]->Pushed(this, dir, dist - 1);
            }

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        }

        ShockTime = 0;
        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;
    }
}
